package com.ljc.controller;

import com.ljc.annotation.ApiLimitedRole;
import com.ljc.annotation.DataLimited;
import com.ljc.constant.AuthRoleConstant;
import com.ljc.entity.BaseResponse;
import com.ljc.entity.UserMoment;
import com.ljc.service.UserMomentsService;
import com.ljc.support.UserSupport;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class UserMomentsController {
    @Resource
    private UserMomentsService userMomentsService;

    @Resource
    private UserSupport userSupport;

    /**
     * 新建用户动态
     */
    @ApiLimitedRole(limitedRoleCodeList = {AuthRoleConstant.ROLE_LV0}) // 用户等级为0，没有权限发送动态
    @DataLimited // 校验传入的数据数值是否正确
    @PostMapping("/user-moments")
    public BaseResponse<String> addUserMoments(@RequestBody UserMoment userMoment) {
        Long userId = userSupport.getCurrentUserId();
        userMoment.setUserId(userId);
        userMomentsService.addUserMoments(userMoment);
        return BaseResponse.success();
    }

    /**
     * 获得该用户的所有被推送的动态
     */
    @GetMapping("/user-subscribed-moments")
    public BaseResponse<List<UserMoment>> getUserSubscribedMoments() {
        Long userId = userSupport.getCurrentUserId();
        List<UserMoment> list = userMomentsService.getUserSubscribedMoments(userId);
        return new BaseResponse<>(list);
    }
}
